package apa.data.samples;

import apa.data.*;
import java.io.*;

public class Select {

	public static void select(BufferedReader in, int[] cols, PrintWriter out)
			throws IOException {
		String[] tokens;
		int rows = 0;
		int nCols = cols.length;
		while ((tokens = IOUtil.readTokens(in)) != null) {
			for (int i = 0; i < nCols - 1; ++i) {
				out.print(tokens[cols[i]]);
				out.print(",");
			}
			out.print(tokens[cols[nCols - 1]]);
			out.println();
			if (++rows % 100 == 0)
				System.out.print("\rRows read: " + rows + "   ");

		}
		System.out.println("\rRows read: " + rows + "   ");
	}

	public static void main(String[] args) {
		if (args.length < 3) {
			System.out
					.println("Usage: java Select inputFile outputFile colNumber ...");
			System.exit(0);
		}
		try {
			BufferedReader input = IOUtil.getReader(args[0]);
			PrintWriter output = IOUtil.getWriter(args[1]);
			int nCol = args.length - 2;
			int[] cols = new int[nCol];
			for (int i = 0; i < nCol; ++i)
				cols[i] = Integer.parseInt(args[i + 2]);
			select(input, cols, output);
			input.close();
			output.close();
		} catch (IOException e) {
			System.out.println(e.toString());
		}
	}
}
